#!/usr/bin/env python
# encoding=utf-8
import datetime
import sys

import pymongo
import requests
from scpy.logger import get_logger

logger = get_logger(__file__)

reload(sys)
sys.setdefaultencoding("utf-8")

CONN = pymongo.MongoClient(host='192.168.31.121', port=27017)


def get_url_content(url):
    content = requests.get(url, timeout=15).content
    return content


def _save_one(collection, data):
    try:
        if 'updateTime' not in data:
            data['updateTime'] = datetime.datetime.utcnow()
        saved = collection.find_one({'_id': data['_id']})
        if saved:
            return 0
        else:
            collection.insert(data)
            return 1
    except Exception, e:
        return -1


def save2db(param):
    coll = CONN['crawler_shixin']['ipBlackList']
    if isinstance(param, dict):
        _save_one(coll, param)
    elif isinstance(param, list):
        for data in param:
            _save_one(coll, data)
    else:
        pass
